package com.pay.game.central.web.mapper;

import com.github.pagehelper.Page;
import com.pay.game.central.web.bean.Store;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


public interface StoreMapper {

    @Insert("insert into pdd_store(id,name,thumb_url,user_id,create_time,`order_index`) " +
            "values(#{id},#{name},#{thumb_url},#{userId},#{crate_time,jdbcType=BIGINT},#{index})")
    void addStore(@Param("id") String id,
                  @Param("name") String name,
                  @Param("thumb_url") String thumb_url,
                  @Param("userId") String userId,
                  @Param("crate_time") long create_time,
                  @Param("index") String index
        );

    @Insert("<script> "+
            "   insert into pdd_store(id,name,create_time) values "+
            "       <foreach collection=\"stores\" item=\"store\" separator=\",\"> "+
            "           (#{store.id},#{store.name},#{store.create_time,jdbcType=BIGINT}) "+
            "       </foreach>"+
            "</script>")
    void addStores(@Param("stores") List<Store> stores);

    @Update("update pdd_store set score=score+#{score,jdbcType=INTEGER} where id=#{id}")
    void addScore(@Param("id") String id, @Param("score") int score);


    @Update("update pdd_store set score=#{score,jdbcType=INTEGER} where id=#{id}")
    void setScore(@Param("id") String id, @Param("score") int score);

    @Update("update pdd_store set score=score-#{score,jdbcType=INTEGER} where id=#{id}")
    void subtractScore(@Param("id") String id, @Param("score") int score);

    @Update("update pdd_store set score=0,used_score=0 where id=#{id}")
    void clearScore(@Param("id") String id);

    @Update("update pdd_store set used_score=used_score+#{value,jdbcType=INTEGER} " +
            "where id=(select store_id from pdd_goods where id=#{id})")
    void addUsedScore(@Param("id") String goodsId, @Param("value") int value);

    @Update("update pdd_store set " +
            "   name=#{name}," +
            "   thumb_url=#{thumb_url}," +
            "   update_time=#{update_time,jdbcType=BIGINT}," +
            "   `index`=#{index}," +
            "   user_id=#{userId} " +
            "where id=#{id}")
    void updateStore(@Param("id") String id,
                     @Param("name") String name,
                     @Param("thumb_url") String thumb_url,
                     @Param("userId") String userId,
                     @Param("update_time") long update_time,
                     @Param("index") String index
    );

    @Update("update pdd_store set order_limit=order_limit-1 where id = (select store_id from pdd_goods where id=#{goodsId})")
    void decreaseOrderLimit(@Param("goodsId") String goodsId);


    @Update("update pdd_store set order_limit=order_limit+1 where id = (select store_id from pdd_goods where id=#{goodsId})")
    void increaseOrderLimit(@Param("goodsId") String goodsId);


    @Update("update pdd_store set user_id=#{userId} where id=#{storeId}")
    void setStoreUserId(@Param("storeId") String storeId,@Param("userId") String userId);

    @Update("update pdd_store set update_time=#{update_time,jdbcType=BIGINT} where id=#{id}")
    void setStoreUDateTime(@Param("id") String id,@Param("update_time")long update_time);

    @Update("update pdd_store set " +
            "   `loop`=true," +
            "   used_score=0," +
            "   start_time=#{time,jdbcType=INTEGER}, " +
            "   settlement_id=#{settlement_id,jdbcType=BIGINT} " +
            "where id=#{id}")
    void setStartScoreParams(@Param("id") String id,
                             @Param("time") long time,
                             @Param("settlement_id") long settlementId);

    @Update("<script>" +
            "   update pdd_store set `loop`=false where id in " +
            "   <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >" +
            "       #{item} " +
            "   </foreach>" +
            "</script>")
    void setStopScoreParams(@Param("ids") List<String> ids);

    @Update("<script> " +
            "   update pdd_store set user_id=#{userId} where id in " +
            "   <foreach collection=\"storeIds\" item=\"item\" open=\"(\" separator=\",\" close=\")\" >" +
            "       #{item} " +
            "   </foreach>" +
            "</script>")
    void setStoresUserId(@Param("storeIds") List<String> storeId,
                         @Param("userId") String userId);

    @Update("update pdd_store set user_id='' where user_id like concat(#{userId},'%')")
    void removeUserId(@Param("userId") String userId);

    @Delete("delete from pdd_store where id=#{id}")
    void deleteById(@Param("id") String id);

    @Select("select id,name,`loop`,score,used_score,settlement_id,user_id from pdd_store where id=#{id}")
    Store queryById(@Param("id") String id);

    @Select("select s.id, s.name,s.`loop`,s.score,s.used_score,s.settlement_id,s.user_id,u.name user_name from pdd_store s, sys_user u where s.id=#{id} and s.user_id=u.id")
    Map<String, Object> queryContainUserInfoById(@Param("id") String id);

    @Select("select id,name,`loop`,score,used_score,settlement_id,user_id from pdd_store")
    Page<Store> queryAll();

    @Select("select * from pdd_store where id like CONCAT('%',#{id},'%')")
    Page<Store> searchById(@Param("id")String id);

    @Select({"<script>",
                    "select s.id, s.name, s.used_score, s.score, u.username  " +
                    "from pdd_store s inner join sys_user u on s.user_id = u.id " +
                    "where  1=1 " +
                    "       <if test=\"id != null and id != ''\">" +
                    "         and s.id like concat(#{id},'%') " +
                    "       </if>"+
                    "       <if test=\"name != null and name != ''\">" +
                    "         and s.name like concat(#{name},'%') " +
                    "       </if>"+
                    "       <if test=\"username != null and username != ''\">" +
                    "         and u.username like concat(#{username},'%') " +
                    "       </if>"
            ,
            "</script>"})
    Page<Map<String,Object>> list(@Param("id") String id,@Param("name") String name, @Param("username") String username);

    @Select("select * from pdd_store where update_time!=0")
    List<Store> getNotSpider();

    @Select("select * from pdd_store where update_time<#{time,jdbcType=BIGINT} ")
    List<Store> getNotUpdate(@Param("time") long time);

    @Select("select ps.`order_index`,ps.id,ps.name,ps.`loop`,ps.score,ps.used_score,ps.settlement_id,ps.user_id,ps.used_score,count(pg.id) goods_size " +
            "from pdd_store ps,pdd_goods pg " +
            "where ps.id=pg.store_id and ps.user_id=#{userId} group by ps.id")
    Page<Store> getUserStore(@Param("userId") String userId);


    @Select("select ps.order_index,ps.id,ps.name,ps.`loop`,ps.score,ps.used_score,ps.settlement_id," +
            "ps.user_id,ps.used_score,count(pg.id) goods_size, sum(pg.loop_times) loop_size," +
            "sum(pg.done_times) done_size, su.order_limit order_size_limit " +
            "from pdd_store ps,pdd_goods pg, sys_user su " +
            "where ps.user_id = su.id and ps.id=pg.store_id and ps.user_id=#{userId} group by ps.id")
    Page<Map> getUserStoreDetail(@Param("userId") String userId);


    @Select("select * from pdd_store " +
            "where user_id=''" +
            "and id like concat('%',#{storeId},'%')")
    List<Store> queryNoUserId(@Param("storeId") String storeId);

    @Select("select id, used_score from pdd_store where user_id = #{userId} and `loop` = 1")
    List<Store> queryRunStoreByUserId(@Param("userId") String userId);
}
